<template>
  <div>
    <h3>运动员进退队审核</h3>
    <div class="playerStatusApply">
      <vxe-toolbar>
        <template #buttons>
          <vxe-input style="float: right;" v-model.trim.lazy="filterName" type="search" placeholder="快捷检索" @input="searchEvent"></vxe-input>
        </template>
      </vxe-toolbar>
      <vxe-table resizable ref="XTable" border max-height="800" stripe highlight-hover-row highlight-current-row :loading="loading"
        :data="playerStatusApplyData">
        <vxe-table-column field="player" title="运动员" sortable width="110"></vxe-table-column>
        <vxe-table-column field="applyEmpName" title="申请人" sortable width="150"></vxe-table-column>
        <vxe-table-column field="status" title="状态请求" sortable width="400">
          <template #default="{ row }">
            <div v-html="row.statusName"></div>
          </template>
        </vxe-table-column>
        <vxe-table-column field="result" title="审核过程" sortable width="100"></vxe-table-column>
        <vxe-table-column field="" title="操作">
          <template #default="{ row }">

            <el-popover
              placement="top-end"
              width="400"
              trigger="click"
              >
              <div style="display: flex;align-items: center;justify-content: space-around;">
                <div style="width: 100px;">驳回原因</div>
                <el-input v-model="row.acceptMemo"></el-input>
                <el-button  @click="sendAction(row)" type="primary">确定</el-button>
              </div>
              <el-link slot="reference" type="primary" icon="iconfont icon-cuo">驳回</el-link>
            </el-popover>

            <el-link @click="repealAction(row)" type="primary" icon="iconfont icon-dui">批准</el-link>
            <el-tooltip class="item" effect="dark" placement="top">
              <div slot="content" style="width: 200px;">
                <h4>{{playerProcess.playerInfo}}</h4>
                <p v-html="playerProcess.processInfo"></p>
              </div>
              <span @mouseenter="popoverShow(row)"><el-link icon="iconfont icon-xiangxixinxi1" type="primary">详细信息</el-link></span>
            </el-tooltip>
            <el-link @click="fileDowload(row)" type="primary" icon="iconfont icon-xiazai1">{{dict_status[row.status.split(',')[1]]}}申请表</el-link>
          </template>
        </vxe-table-column>
      </vxe-table>
      <vxe-pager :loading="loading" :current-page="page.currentPage" :page-size="page.pageSize" :total="page.totalResult"
        :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']" @page-change="handlePageChange">
      </vxe-pager>
    </div>
    <vxe-modal v-model="printModal" title="预览" height="800" show-zoom resize width="600" show-footer>
      <template #default>
        <playerProcessStatusPrint   id="myPrint" :currentPrint="currentPrint" />
      </template>
      <template #footer>
        <el-button @click="printEvent" type="primary" icon="el-icon-printer" size="mini">打印</el-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
  import {playerAcceptDefault,player_status,playerAcceptUpdate,playerApplyDetail,process_playerInfo} from "@/api/person.js"
  import {currentRolePlayer,player_process_result} from "@/api/dict.js"
  import playerProcessStatusPrint from "@/components/print/playerProcessStatusPrint.vue"
  export default {
    components: {
      playerProcessStatusPrint,
    },
    data() {
      return {
        visible: true,
        value: '',
        playerStatusApplyData: [],
        defaultPlayerStatusApplyData: [],
        dict_player: {},
        dict_result: {},
        page: {
          currentPage: 1,
          pageSize: 10,
          totalResult: 0
        },
        loading: false,
        filterName: '',
        searchProps: [],
        dict_status: {
            "3": "预备",
            "4": "入队-试训",
            "1": "入队-转正",
            "6": "入队",
            "5": "上级单位集训试训",
            "2": "离队"
        },
        formData: {
          prioritySort: 10,
          currentStatusId: '',
          playerId: '',
          resultId: '',
          status: [],
          goalStatusId: '',
          exeDate: '',
        },
        statusOptions: [
          {
            value: '0',
            label: '入队/在训',
            children: [
              {
                value: '4',
                label: '试训',
              },
              {
                value: '1',
                label: '转正',
              },
              {
                value: '5',
                label: '上级单位集训试训',
              }
            ],
          },
          {
            value: '3',
            label: '预备',
            children: [
              {
                value: '3',
                label: '预备',
              },
            ],
          },
          {
            value: '2',
            label: '离队',
            children: [
              {
                value: '2',
                label: '离队',
              },
            ],
          }
        ],
        rules: {
          playerId: [{
            required: true,
            message: '这是必填项',
            trigger: 'blur'
          }, ],
        },
        startStatus: '',
        cascaderStatus: ['0','4'],
        edit: false,
        modalTitle: '运动员状态变更',
        flowId: '',
        playerProcess: {playerInfo:'',processInfo:''},
        printModal: false,
        currentPrint: {},
      }
    },
    created() {
      this.init_dict().then(()=>{
        this.getPlayerAccept()
      })
    },
    methods: {
      printEvent() {
        const divEl = document.getElementById('myPrint')
        this.$XPrint({
          sheetName: '',
          content: divEl.innerHTML
        })
      },
      fileDowload(row) {
        process_playerInfo({playerId:row.playerId}).then(res=>{
          this.currentPrint = {}
          if(res.code === 200) {
            this.currentPrint = res.data
          }
          this.currentPrint.title = this.dict_status[row.status.split(',')[1]]
          this.printModal = true
        })
      },
      popoverShow(row) {
        playerApplyDetail({flowId:row.flowId,playerId:row.playerId}).then(res=>{
          if(res.code===200){
            this.playerProcess = res.data
          }else{
            this.playerProcess.playerInfo = '空'
          }
        })
      },
      // 批准
      repealAction(row) {
        playerAcceptUpdate({flowId:row.flowId,resultId:3}).then(res=>{
          if(res.code === 200) {
            this.$message.success('请求批准成功！')
            row.resultId = 3
            row.result = this.dict_result[row.resultId]
          }
        })
      },
      // 驳回
      sendAction(row) {
        let data = {}
        data.flowId = row.flowId
        data.resultId = 5
        if(row.acceptMemo) data.acceptMemo = row.acceptMemo
        playerAcceptUpdate(data).then(res=>{
          if(res.code === 200) {
            this.$message.success('请求驳回成功！')
            row.resultId = 5
            row.result = this.dict_result[row.resultId]
            this.visible = false
          }
        })
      },
      getPlayerAccept() {
        playerAcceptDefault({defaultBeginDate:this.$moment().add(-3,'y').format('YYYY-MM-DD hh:mm:ss')}).then(res=>{
          if(res.code === 200) {
            res.data.forEach(item=>{
              item.player = this.dict_player[item.playerId]
              item.result = this.dict_result[item.resultId]
              if(item.status){
                let statusArr = item.status.split(',')
                let arr = []
                statusArr.forEach(item=>{
                  arr.push(this.dict_status[item])
                })
                item.statusName = arr.join(' <i class="iconfont icon-jiantou"></i> ')
              }
            })
            this.searchProps = Object.keys(res.data[0])
            this.defaultPlayerStatusApplyData = res.data
          }else{
            this.defaultPlayerStatusApplyData = []
          }
        }).then(()=>{
          this.findList()
        })
      },
      init_dict() {
        return new Promise((resolve,reject)=>{
          player_process_result().then(res=>{
            this.dict_result = res
          })
          currentRolePlayer().then(res=>{
            this.dict_player = res
            resolve()
          })
        })
      },
      searchEvent() {
        const filterName = this.$XEUtils.toValueString(this.filterName).trim().toLowerCase()
        if (filterName) {
          const filterRE = filterName
          const searchProps = this.searchProps
          const rest = this.defaultPlayerStatusApplyData.filter(item => searchProps.some(key => this.$XEUtils.toValueString(item[
            key]).toLowerCase().indexOf(filterName) > -1))
          this.playerStatusApplyData = rest.map(row => {
            const item = Object.assign({}, row)
            searchProps.forEach(key => {
              item[key] = this.$XEUtils.toValueString(item[key]).replace(filterRE, match => match)
            })
            return item
          })
        } else {
          this.playerStatusApplyData = this.defaultPlayerStatusApplyData
          this.findList()
        }
      },
      handlePageChange({
        currentPage,
        pageSize
      }) {
        this.page.currentPage = currentPage
        this.page.pageSize = pageSize
        this.findList()
      },
      sortChange() {
        this.page.currentPage = 1
        this.page.pageSize = this.defaultDataList.length
        this.findList()
      },
      findList() {
        this.loading = true
        setTimeout(() => {
          this.loading = false
          this.page.totalResult = this.defaultPlayerStatusApplyData.length
          this.playerStatusApplyData = this.defaultPlayerStatusApplyData.slice((this.page.currentPage - 1) * this.page
            .pageSize, this.page.currentPage * this.page.pageSize)
        }, 300)
      },
    },
  }
</script>

<style lang="scss" scoped>
  .playerStatusApply{
    background-color: #FFFFFF;
    padding: 10px;
    a{
      margin-right: 10px;
    }
  }
</style>
